﻿/*using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using Stemmer;

namespace Common
{
    public class Catalog
    {
        /// <summary>
        /// Key : Word
        /// Value : WordData
        /// </summary>
        public Hashtable WordDatas = new Hashtable(); // <Word , WordData >
        Hashtable Documents = new Hashtable(); // <docID, Document>

        //For search engine scoring calculations.
        public int NumberOfDocumentsIndexed = 0;
        public int[] SumOfFieldLength; //array of field length sums

        HebrewStemmer Stemmer;
        object lockObject = new object();

        public Catalog(string prefixFilePath)
        {
            Stemmer = new HebrewStemmer(prefixFilePath);
            Document.Section ft = new Document.Section();
            SumOfFieldLength = new int[Enum.GetNames(ft.GetType()).Length];
        }

        /// <summary>
        /// Merges Catalog into this one.
        /// </summary>
        /// <param name="Catalog"></param>
        public void MergeCatalog(Catalog Catalog)
        {
            //For each Word in catalog
            foreach (DictionaryEntry WordWordData in Catalog.WordDatas)
            {
                //For each document that contains the word
                foreach (DictionaryEntry DocIDPosList in
                    ((WordData)WordWordData.Value).Documents)
                {
                    //For each word pos in document
                    foreach (WordPos wp in DocIDPosList.Value as List<WordPos>)
                    {
                        //Add to current catalog.
                        lock (lockObject)
                        {
                            AddWord(WordWordData.Key as Word, (long)(DocIDPosList.Key), wp);
                        }
                    }
                }
            }
            //Statistics
            NumberOfDocumentsIndexed += Catalog.NumberOfDocumentsIndexed;
            lock (lockObject)
            {
                for (int i = 0; i < SumOfFieldLength.Length; ++i)
                {

                    SumOfFieldLength[i] += Catalog.SumOfFieldLength[i];
                }
            }
            //Add documents to index.
            lock (lockObject)
            {
                foreach (DictionaryEntry docIDoc in Catalog.Documents)

                    AddDocument(docIDoc.Value as Document);
            }
        }

        public Document GetDocumentByID(long id)
		{
			if (!Documents.Contains(id)) return null;
			return Documents[id] as Document;
		}

		public void AddDocument(Document doc)
		{
			long ID = doc.Id;
			if (Documents.Contains(ID)) return;
			Documents.Add(ID, doc);
		}

        public void AddWord(Word word, long DocumentID, WordPos wordPosition)
        {
            if (word.Text == String.Empty) 
                return;
            WordPos WordPosition = new WordPos(wordPosition);
            List<Word> wordList = GetWordListFromStringList(Stemmer.GetStemmedWords(word.Text));
            //wordList.Add(word);
            foreach (Word Word in wordList)
            {
                if (wordList[0] == Word) //This is original word
                    WordPosition.IsOriginal = true;
                else
                    WordPosition.IsOriginal = false;

                if (!WordDatas.ContainsKey(Word)) //This is a new word.
                {
                    WordData WordData = new WordData();
                    //WordData.SearchTerms = GetSearchTermsContainingWord(Word); //create searchterm list containing word.
                    List<WordPos> WordPosList = new List<WordPos>(); //Create a new list of word positions.
                    WordPosList.Add(new WordPos(WordPosition));
                    WordData.Documents.Add(DocumentID, WordPosList);
                    WordDatas.Add(Word, WordData);
                }
                else //Word is already in index
                {
                    WordData WordData = WordDatas[Word] as WordData; 
                    if (WordData.Documents.Contains(DocumentID)) //Document is already in index
                    {   //Add a new position of the word, if its not already in there (for merges)
                        if (!((WordDatas[Word] as WordData).Documents[DocumentID] as List<WordPos>).Contains(WordPosition))
                            ((WordDatas[Word] as WordData).Documents[DocumentID] as List<WordPos>).Add(new WordPos(WordPosition));
                    }
                    else //Need to add this document and word Position.
                    {
                        List<WordPos> WordPosList = new List<WordPos>();
                        WordPosList.Add(new WordPos(WordPosition));
                        (WordDatas[Word] as WordData).Documents.Add(DocumentID, WordPosList);
                    }
                }
            }
        }

        public List<Word> GetWordListFromStringList(List<string> StringList)
        {
            List<Word> ret = new List<Word>();
            foreach (string s in StringList)
                ret.Add(new Word(s));
            return ret;
        }

       
      

        /// <summary>
        /// For a particular searchword, 
        /// Gets a hash of Key: Document, Value : List of WordPos for that word in the document.
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public Hashtable GetDocumentListPerWord(Word searchWord, bool ExactMatch)
        {
            Hashtable retval = new Hashtable();
            Hashtable WordDocuments;
            List<Word> allCombinations = new List<Word>();
            if (ExactMatch)
            {
                if (WordDatas.ContainsKey(searchWord))
                {
                    //Clone the hash in order not to iterate over the one that's being modified.
                    WordDocuments = (Hashtable)(WordDatas[searchWord] as WordData).Documents.Clone();

                    foreach (DictionaryEntry e in WordDocuments)
                    {
                        if (retval.Contains(e.Key)) //Our list already contains this document. 
                        {
                            foreach (WordPos w in WordDocuments[e.Key] as List<WordPos>) //Add this term's wordpos list, only if word is original
                            {
                                if (w.IsOriginal)
                                    (retval[e.Key] as List<WordPos>).Add(w);
                            }
                        }
                        else
                        {
                            List<WordPos> newList = new List<WordPos>();
                            foreach (WordPos w in e.Value as List<WordPos>) //Add this term's wordpos list, only if word is original.
                            {
                                if (w.IsOriginal)
                                    newList.Add(w);
                            }
                            if (newList.Count > 0)
                                retval.Add(e.Key, newList); //New document, add it to results array.
                        }
                    }
                }//If Contains Word

            } //If ExactMatch
            else
            {
                allCombinations = GetWordListFromStringList(Stemmer.GetStemmedWords(searchWord.Text));

                foreach (Word Word in allCombinations)
                {
                    if (WordDatas.ContainsKey(Word))
                    {
                        WordDocuments = (Hashtable)(WordDatas[Word] as WordData).Documents.Clone();
                        foreach (DictionaryEntry e in WordDocuments)
                        {
                            if (retval.Contains(e.Key)) //Our list already contains this document. 
                            {
                                foreach (WordPos w in WordDocuments[e.Key] as List<WordPos>) //Add this term's wordpos list .
                                {
                                    (retval[e.Key] as List<WordPos>).Add(w);
                                }
                            }
                            else
                            {
                                List<WordPos> newList = new List<WordPos>(e.Value as List<WordPos>);
                                retval.Add(e.Key, newList); //New document, add it to results array.

                            }
                        }
                    }
                }
            }
            return retval;// (retval.Count == 0) ? null : retval;
        }

        private List<string> GetSearchTermsContainingWord(Word Word)
        {
            throw new NotImplementedException();
        }

        private int MaxDistanceBweteenWords(Word w1, Word w2, long DocId, Document.Section section)
        {
            var wd1 = WordDatas[w1] as WordData;
            var wd2 = WordDatas[w2] as WordData;

            var wp1 = wd1.Documents[DocId] as List<WordPos>;
            var wp2 = wd2.Documents[DocId] as List<WordPos>;

            int minW1, minW2, maxW1, maxW2;
            MinMaxPosition(wp1, out minW1, out maxW1, section);
            MinMaxPosition(wp2, out minW2, out maxW2, section);
            return Math.Max((maxW2 - minW1), (maxW1 - minW2));
        }
        private void MinMaxPosition(List<WordPos> wpList, out int min, out int max, Document.Section section)
        {
            min = int.MaxValue;
            max = int.MinValue;
            foreach (WordPos wp in wpList)
            {
                if (wp.Section != section) continue;
                var pos = wp.Position;
                if (pos < min) min = pos;
                if (pos > max) max = pos;
            }
        }


    }
}
*/